//
//  UIBarButtonItem+Extension.swift
//  微博(Swift)
//
//  Created by V on 15/12/27.
//  Copyright © 2015年 V. All rights reserved.
//

import UIKit

extension UIBarButtonItem{

    // 在分类里面添加构造函数，用便利构造函数
    // Swift 函数的参数可以给定一个默认值，外界可以不传这个参数，就使用默认值
    convenience init(imageName: String? = nil, title: String? = nil, target: AnyObject?, action: Selector) {
        self.init()
        let btn: UIButton = UIButton()
        btn.addTarget(target, action: action, forControlEvents: .TouchUpInside)
        if imageName != nil{
            btn.setImage(UIImage(named: imageName!), forState: .Normal)
            // imageName 不加 ！ 没有高亮状态
            btn.setImage(UIImage(named: "\(imageName!)_highlighted"), forState: .Highlighted)
        }
        if title != nil{
            btn.setTitle(title, forState: .Normal)
            btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Highlighted)
            btn.setTitleColor(UIColor(white: 80/255, alpha: 1), forState: UIControlState.Normal)
            btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        }
        
        btn.sizeToFit()
        customView = btn
    }
}
